<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
          <el-option
            v-for="dict in task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="执行班次" prop="taskShift">
        <el-select v-model="queryParams.taskShift" placeholder="请选择执行班次" clearable>
          <el-option
            v-for="dict in execution_shift_1"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务执行日期" prop="taskDate">
        <el-date-picker clearable
          v-model="queryParams.taskDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择任务执行日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备id，关联设备表" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备id，关联设备表"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['service:atmTask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:atmTask:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:atmTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['service:atmTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="atmTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID，自增" align="center" prop="id" />
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :options="task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="执行班次" align="center" prop="taskShift">
        <template #default="scope">
          <dict-tag :options="execution_shift_1" :value="scope.row.taskShift"/>
        </template>
      </el-table-column>
      <el-table-column label="任务执行日期" align="center" prop="taskDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.taskDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开锁方式" align="center" prop="lock" />
      <el-table-column label="可开锁次数" align="center" prop="lockAllowTimes" />
      <el-table-column label="加钞金额" align="center" prop="amount" />
      <el-table-column label="加钞券别及金额" align="center" prop="denomList" />
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="取消类型" align="center" prop="cancelType" />
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="设备id，关联设备表" align="center" prop="deviceId" />
      <el-table-column label="银行报修时间" align="center" prop="timeFromBank" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.timeFromBank, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否紧急任务" align="center" prop="important">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.important"/>
        </template>
      </el-table-column>
      <el-table-column label="是否清机" align="center" prop="needClearing">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.needClearing"/>
        </template>
      </el-table-column>
      <el-table-column label="物品，关联zone_supplies表，用逗号分隔" align="center" prop="suppliersList" />
      <el-table-column label="任务备注描述" align="center" prop="remark" />
      <el-table-column label="上次任务备注描述" align="center" prop="lastRemark" />
      <el-table-column label="锁具状态" align="center" prop="lockStatus">
        <template #default="scope">
          <dict-tag :options="lock_status" :value="scope.row.lockStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="已开锁次数" align="center" prop="lockOpenTimes" />
      <el-table-column label="已闭锁次数" align="center" prop="lockCloseTimes" />
      <el-table-column label="回线钞箱个数" align="center" prop="returnBoxCounts" />
      <el-table-column label="额外任务到达时间" align="center" prop="extraTaskTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.extraTaskTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="额外任务离开时间" align="center" prop="extraTaskLeaveTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.extraTaskLeaveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['service:atmTask:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['service:atmTask:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改atm任务信息列对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="atmTaskRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务类型" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务类型">
            <el-option
              v-for="dict in task_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行班次" prop="taskShift">
          <el-select v-model="form.taskShift" placeholder="请选择执行班次">
            <el-option
              v-for="dict in execution_shift_1"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务执行日期" prop="taskDate">
          <el-date-picker clearable
            v-model="form.taskDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择任务执行日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开锁方式" prop="lock">
          <el-input v-model="form.lock" placeholder="请输入开锁方式" />
        </el-form-item>
        <el-form-item label="加钞金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入加钞金额" />
        </el-form-item>
        <el-form-item label="设备id，关联设备表" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id，关联设备表" />
        </el-form-item>
        <el-form-item label="银行报修时间" prop="timeFromBank">
          <el-date-picker clearable
            v-model="form.timeFromBank"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择银行报修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否紧急任务" prop="important">
          <el-select v-model="form.important" placeholder="请选择是否紧急任务">
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否清机" prop="needClearing">
          <el-select v-model="form.needClearing" placeholder="请选择是否清机">
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品，关联zone_supplies表，用逗号分隔" prop="suppliersList">
          <el-input v-model="form.suppliersList" placeholder="请输入物品，关联zone_supplies表，用逗号分隔" />
        </el-form-item>
        <el-form-item label="任务备注描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AtmTask">
import { listAtmTask, getAtmTask, delAtmTask, addAtmTask, updateAtmTask } from "@/api/service/atmTask"

const { proxy } = getCurrentInstance()
const { task_status, lock_status, sys_yes_no, task_type, execution_shift_1 } = proxy.useDict('task_status', 'lock_status', 'sys_yes_no', 'task_type', 'execution_shift_1')

const atmTaskList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskType: null,
    taskShift: null,
    taskDate: null,
    deviceId: null,
  },
  rules: {
    taskType: [
      { required: true, message: "任务类型不能为空", trigger: "change" }
    ],
    taskShift: [
      { required: true, message: "执行班次不能为空", trigger: "change" }
    ],
    taskDate: [
      { required: true, message: "任务执行日期不能为空", trigger: "blur" }
    ],
    lock: [
      { required: true, message: "开锁方式不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "加钞金额不能为空", trigger: "blur" }
    ],
    denomList: [
      { required: true, message: "加钞券别及金额不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "任务状态不能为空", trigger: "change" }
    ],
    deviceId: [
      { required: true, message: "设备id，关联设备表不能为空", trigger: "blur" }
    ],
    important: [
      { required: true, message: "是否紧急任务不能为空", trigger: "change" }
    ],
    suppliersList: [
      { required: true, message: "物品，关联zone_supplies表，用逗号分隔不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询atm任务信息列列表 */
function getList() {
  loading.value = true
  listAtmTask(queryParams.value).then(response => {
    atmTaskList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    taskType: null,
    taskShift: null,
    taskDate: null,
    lock: null,
    lockAllowTimes: null,
    amount: null,
    denomList: null,
    status: null,
    cancelType: null,
    cancelReason: null,
    deviceId: null,
    timeFromBank: null,
    important: null,
    needClearing: null,
    suppliersList: null,
    remark: null,
    lastRemark: null,
    lockStatus: null,
    lockOpenTimes: null,
    lockCloseTimes: null,
    returnBoxCounts: null,
    extraTaskTime: null,
    extraTaskLeaveTime: null,
    createTime: null,
    createBy: null,
    deliverTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("atmTaskRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加atm任务信息列"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getAtmTask(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改atm任务信息列"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["atmTaskRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAtmTask(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAtmTask(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除atm任务信息列编号为"' + _ids + '"的数据项？').then(function() {
    return delAtmTask(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/atmTask/export', {
    ...queryParams.value
  }, `atmTask_${new Date().getTime()}.xlsx`)
}

getList()
</script>
